<?php
/**
 * True Gallery back-end pictures template file
 *
 * @version 2.5
 * @author Alexey N. Brin (userxp@joomlaportal.ru)
 * @homepage http://truegallery.ru 
 * @filename admin.pictures.tpl.php
 * @package True Gallery
 * @copyright (C) 2008-2009 by joomlatune.ru
 * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
 * 
 * THIS GALLERY is an EVOLUTION of AkoGallery -> PonyGallery -> DatsoGallery
 *
 * If you fork this to create your own project, 
 * please make a reference to True Gallery someplace in your code 
 * and provide a link to http://www.joomlatune.ru
 *
 **/

defined('_VALID_MOS') or defined('_JEXEC') or die('Direct Access to this location is not allowed.');
getPlugin('fancybox');
?>
<form action="<?php echo TRUE_INDEX2; ?>&view=<?php echo $view; ?>" method="post" name="adminForm" id="adminForm">
	<table cellpadding="4" cellspacing="0" border="0" class="adminheading">
		<tr>
			<!--th class="tg_adminheader tg_pictures">
				<?php echo JText::_('TG_PICS_MANAGER_TITLE'); ?>
			</th-->
			<td><?php echo JText::_('TG_FILTER'); ?>:
				<?php if( count($where) ) echo JTuneLayout::getImage( 'no_download' ); ?>
				<?php //Button2( JText::_('Filter'), 'fancybox-button', 'readmore', TRUE_INDEX2.'&view=pictures&task=showfilter&ax=1&no_html=1' ) ;?>
				<?php if( count($where) ) { ?>
				<a href="<?php echo TRUE_INDEX2.'&view='.$view.'&task=clearstate'; ?>">Clear</a>
				<?php } ?>
			</td>
			<td>Sorting:
				<?php //if( count($where) ) echo JTuneLayout::getImage( 'no_download' ); ?>
				<?php //Button2( JText::_('Clear Sorting'),'fancybox-button-clear','end' ) ;?>
				<?php echo '&nbsp;&nbsp;'.str_replace( array('|','='), array(' | ',' = '), $sort ); ?>
			</td>
			<td>
				<input type="checkbox" <?php echo $allow_overlib; ?> id="showoverlib" name="showoverlib" />
				<label for="showoverlib"><?php echo JText::_('TG_SHOW_OVERLIB'); ?></label>
			</td>
		</tr>
	</table>

	<div id="formContent">
		<table cellpadding="4" cellspacing="0" border="0" width="100%" class="adminlist">
			<tr>
				<th align="right" colspan="16">
					<?php echo $pageNav->writePagesLinks(); ?> 
					<?php echo $pageNav->writeLimitBox(); ?>
					<?php echo $pageNav->writePagesCounter(); ?>
				</th>
			</tr>
			<tr>
				<th width="40">
					<?php $this->_writeSortableField( 'id', '# ID' ); ?>
				</th>
				<th width="20"><input type="checkbox" name="toggle" value="" onclick="checkAll(<?php echo count($rows); ?>);" /></th>
				<th>
					<?php $this->_writeSortableField( 'a.imgtitle', JText::_('TG_TITLE') ); ?>
				</th>
				<th>
					<?php $this->_writeSortableField( 'category', JText::_('TG_CATEGORY') ); ?>
				</th>
				<th width="20" align="center">
					<?php $this->_writeSortableField( 'a.width', substr( JText::_('TG_ORG_WIDTH'),0,2) ); ?>
					
				</th>
				<th width="20" align="center">
					<?php $this->_writeSortableField( 'a.height', substr( JText::_('TG_ORG_HEIGHT'),0,2) ); ?>
					
				</th>
				<th width="20" align="center" title="<?php echo JText::_('TG_ORIENTATION'); ?>">
					<?php $this->_writeSortableField( 'orient', substr(JText::_('TG_ORIENTATION'),0,6) ); ?>
				</th>
				<th width="50" align="center" title="<?php echo JText::_('TG_HITS'); ?>">
					<?php $this->_writeSortableField( 'a.hits', substr(JText::_('TG_HITS'),0,10) ); ?>.
				</th>
				<th width="50">
					<?php $this->_writeSortableField( 'sortrating', JText::_('TG_RATING') ); ?>
				</th>
				<th width="30" align="center">
					<?php echo JText::_('TG_ORDER'); ?>
				</th>
				<th width="40" align="center">
					<a href="javascript: saveorder( <?php echo count($rows) - 1; ?> )">
						<?php echo JTuneLayout::getImage( 'filesave' ); ?>
					</a>
				</th>
				<th width="35" title="<?php echo JText::_('TG_PUBLISHED'); ?>">
					<?php $this->_writeSortableField( 'a.published', substr(JText::_('TG_PUBLISHED'),0,8) ); ?>.
				</th>
				<th width="30">
					<?php $this->_writeSortableField( 'a.approved', JText::_('TG_APPROWED') ); ?>
				</th>
				<th width="70" align="center">	
					<?php $this->_writeSortableField( 'owner', JText::_('TG_AUTHOR_OWNER') ); ?>
				</th>
				<th width="30" align="center">
					<?php $this->_writeSortableField( 'a.useruploaded', JText::_('TG_TYPE') ); ?>
				</th>
				<th width="100" align="center">
					<?php $this->_writeSortableField( 'a.imgdate', JText::_('TG_DATE_ADD') ); ?>
				</th>
			</tr>
			<?php
			$pid_codes = "pid_codes=[];\n";
			for( $i = 0, $n = count($rows); $i < $n; $i++) {
				$row = &$rows[$i];
				
				if($row->avgvotes > 0) {
					$frating = number_format($row->avgvotes, 2) .' ('.$row->voices.')';
				} else {
					$frating = '---';//JText::_('TG_NO_VOTES');
				}
				
				$tr_class  = 'row0 trover';
				$tr_class .= $row->approved ? ' approve' : ' reject';
				$tr_class .= $row->published ? ' publish' : ' unpublish';
				
				// for any case (insurance)
				if( !$row->width || !$row->height || !$row->filesize ) {
					$info		= getimagesize( TG_ORIGINALSPATH . $row->catid . DSX . $row->imgfilename );
					$size		= filesize( TG_ORIGINALSPATH . $row->catid . DSX . $row->imgfilename );
					$type		= array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG');
					
					$row->width			= $info[0];
					$row->height		= $info[1];
					//$row->type			= $type[$info[2]];
					$row->filesize		= format_filesize($size);
					
					$sql = 'UPDATE '.TG_TBLPREFIX." SET width=$row->width, height=$row->height, filesize=$size WHERE id=$row->id";
					$db->setQuery($sql);
					$db->query();
					//xdump( $db->getQuery() );
				}
				
				$row->type			= array_pop( explode( '.', $row->imgfilename ) );
				$row->orientation	= $row->width<$row->height?'|':'&#0151;';
				
				$row->src			= TG_THUMBSWAY.$row->catid.'/'.$row->imgfilename;
				$row->description	= $row->imgtext;
				
				// preparing and writting overlib template into JS array
				$pid_codes .= "pid_codes[$i] = '".getOverlibLayout($row)."'\n";
				
				$i_title	= $row->imgtitle . ' [' . $row->category . '], ' . JText::_('Author') . ': ' . $row->owner;
				$i_href		= TG_PICTURESWAY . $row->catid . '/' . $row->imgfilename;
				$i_view		= JTuneLayout::getImage('001_03','images','.gif');
				?>
				
			<tr class="<?php echo $tr_class; ?>" >
				<td align="right"><?php echo $row->id; ?></td>
				<td align="center"><input type="checkbox" id="cb<?php echo $i; ?>" name="id[]" value="<?php echo $row->id; ?>" onclick="isChecked(this.checked);" /></td>
				<td>
					<a class="group" rel="group" title="<?php echo $i_title; ?>" href="<?php echo $i_href; ?>"><?php echo $i_view ?></a>
					<a style="display:inline;" href="<?php echo TRUE_INDEX2.'&view=pictures&task=edit&id='.$row->id ?>" ><?php echo $row->imgtitle; ?></a>
				</td>
				<td><?php 
					$href = TRUE_INDEX2.'&view=categories&task=edit&catid='.$row->catid;
					echo '<a href="'.$href.'">&nbsp;'.$row->category.'</a>';
				?></td>
				
				<td align="center"><?php echo $row->width; ?></td>
				<td align="center"><?php echo $row->height; ?></td>
				<td align="center"><?php echo $row->orientation; ?></td>
				<td align="center"><?php echo $row->hits; ?></td>
				<td align="center"><?php echo $frating; ?></td>
				<td align="center">
					<?php echo $pageNav->orderUpIcon( $i, ($row->catid == @$rows[$i-1]->catid) ); ?>
					<?php echo $pageNav->orderDownIcon( $i, $n, ($row->catid == @$rows[$i+1]->catid) ); ?>
				</td>
				<td align="center">
					<input type="text" name="order[]" size="5" value="<?php echo $row->ordering; ?>" class="text_area" style="text-align:center" />
				</td>
				<td align="center">
					<?php // $state=1, $id=0, $name='publish', $redirect=true, $image=true
					echo $this->publish( $row->published, $row->id, 'publish', false ); ?>
				</td>
				<td align="center">
					<?php echo $this->approve( $row->approved, $row->id, 'approve', false ); ?>
				</td>
				<td align="center"><b><?php echo $row->owner; ?></b></td>
				<td align="center">
					<?php if($row->useruploaded) { ?>
					<img src="<?php echo JTuneConfig::getCfg('live_site'); ?>/includes/js/ThemeOffice/users.png" title="<?php echo JText::_('TG_USER_UPLOAD'); ?>">
					<?php } else { ?>
					<img src="<?php echo JTuneConfig::getCfg('live_site'); ?>/includes/js/ThemeOffice/credits.png" title="<?php echo JText::_('TG_ADMIN_UPLOAD'); ?>">
					<?php } ?>
				</td>
				<td width="10%" align="center"><?php echo strftime( "%d.%m.%y %H:%M", $row->imgdate ); ?></td>
			</tr><?php 
			} ?>
			<tr>
				<th align="right" colspan="16">
					<?php echo $pageNav->writePagesLinks(); ?> 
					<?php echo $pageNav->writePagesCounter(); ?>
				</th>
			</tr>
			<tr>
				<td align="center" colspan="16"></td>
			</tr>
		</table>
		
		<input type="hidden" name="option" value="<?php echo TG_CMP; ?>" />
		<input type="hidden" name="task" value="" />
		<input type="hidden" name="view" value="pictures" />
		<input type="hidden" name="boxchecked" value="0" />
		
		<!--input type="hidden" name="sort" value="" />
		<input type="hidden" name="sorder" value="" /-->
		
		<?php if( JOOMLA_JVERSION=='1.5' ) { ?>
		<!--input type="hidden" name="limit" value="<?php //echo $pageNav->limit;?>" /-->
		<input type="hidden" name="limitstart" value="<?php echo $pageNav->limitstart;?>" />
		<?php } ?>
	</div>	
</form>

<script type="text/javascript">
<!--
// pictures where filter
//var pic_where_filter = '<?php echo $pic_where_filter; ?>';

function submitbutton(pressbutton) {
	//console.log( pressbutton );
	if( pressbutton !== 'add' ) {
		submitform(pressbutton);
	}
}

function submitform(pressbutton){
	if (pressbutton) {
		document.adminForm.task.value=pressbutton;
	}
	if (typeof document.adminForm.onsubmit == "function") {
		document.adminForm.onsubmit();
	}
	document.adminForm.submit();
}

// *** BEGIN SORTING HAMDLER
$j( "a.sorted_link" ).each(function(index, elm) {
	var form = document.adminForm;
	if( form.sort == undefined ) {
		$j(form).append('<input type="hidden" name="sort" value="<?php echo $sort; ?>" />');
	}
	
	$j(this).click(function(e) {
		var s = form.sort.value.split('|');
		var name = elm.id.replace( '<?php echo $userStatePrefix; ?>', '' );
		var search = '';
		var current_field = name+'=ASC|';
		for( i=0; i < s.length; i++ ) {
			if( s[i] != '' ) {
				var walle = s[i].split('=');
				if( walle[0] == name ) {
					switch( walle[1] ) {
						default : 
							v = 'ASC';
						break;
						case 'ASC':
							v = 'DESC';
						break;
						case 'DESC':
							v = '';
						break;
					}
					if( v == '' ) current_field = '';
					else current_field = walle[0]+'='+v+'|';
				}
				else if( walle[1] != '' ) {
					search += walle[0]+'='+walle[1]+'|';
				}
			}
		}
		search = current_field+search;
		form.sort.value = search;
		form.submit();
	});
});

$j("td#toolbar-clear a").attr('onclick','').click(function() {
	var form = document.adminForm;
	if( form.sort == undefined ) {
		$j(form).append('<input type="hidden" name="sort" value="" />');
	}
	form.sort.value ='';
	form.submit();
});
// *** END SORTING BLOCK

function editEntry(number) {
	var elm = document.getElementById('cb'+number);
	if( elm ) {
		elm.checked = true;
	}
}

$j("td#toolbar-delete a").attr('onclick','').click(function(){
	var id = '';
	if( document.adminForm.boxchecked.value >0 ) {
		//$j("input:checked").each(function(){
		$j("input:checked[name=id\\[\\]]").each(function(){
			if( id!='' ) id += ',';
			id += $j(this).val();
			//console.log(this);
		});
		//var cbs = $j("input:checked[name=id\\[\\]]");
		//console.log( cbs );
		
		if( window.confirm( 'This will delete all images in selected folders. Are you sure?' ) ) {
			// what about science fiction? :)
			var o = $j('<a href=""></a>' ); // let's create virtual A-object
			o.attr('href','<?php echo TRUE_INDEX2.'&view=pictures&task=delete&id=';?>'+id+'&'+TG_AX);
			o.fancybox({
				'zoomOpacity'			: false,
				'overlayShow'			: true,
				'zoomSpeedIn'			: 300,
				'zoomSpeedOut'			: 300,
				//'frameWidth'			: 550,
				//'frameHeight'			: 415,
				'hideOnContentClick'	: false,
				'callbackOnShow'		: function() {
					$j(this).blur();
					stopLoadingIcon();
				},
				'callbackOnClose'		: function() {
					stopLoadingIcon();
				}
			});
			o.click();// clicking! :)
		}
		
	} else {
		alert( 'Select anything, please!' );
	}
	
});

// fancy image preview button handler
$j("a.group").fancybox({
	'zoomOpacity'			: true,
	'easingIn'				: 'easeOutBack',
	'easingOut'				: 'easeInBack',
	'zoomSpeedIn'			: 700,
	'zoomSpeedOut'			: 700
});

// RENAME IMAGES
//$j("td#toolbar-rename a").attr('href','<?php echo TRUE_INDEX2.'&view=pictures&task=showrenameimages&filter='.htmlentities($pic_where_filter).'&'.TG_AX; ?>');
$j("td#toolbar-rename a").attr('href','index.php');
$j("td#toolbar-rename a").attr('onclick','').fancybox({
	ajax:				{
		type: 'POST',
		data: {
			option: TG_CMP,
			view: 'pictures',
			task: 'showrenameimages',
			filter: '<?php echo htmlentities($pic_where_filter); ?>',
			ordering: '<?php echo htmlentities($sortorder); ?>',
			ax: 1,
			no_html: 1
		}
	},
	zoomOpacity			: true,
	overlayShow			: true,
	zoomSpeedIn			: 500,
	zoomSpeedOut		: 500,
	frameHeight			: 400, // min-height
	hideOnContentClick	: false,
	callbackOnShow		: function() {
		stopLoadingIcon();
		$j("a.fancybox-button").blur();
	}
});

// FILTERING LISTING
$j("td#toolbar-filter a").attr('href','<?php echo TRUE_INDEX2.'&view=pictures&task=showfilter&'.TG_AX; ?>');
$j("td#toolbar-filter a").attr('onclick','').fancybox({
	//ajax:				{type:'POST'},
	zoomOpacity			: true,
	overlayShow			: true,
	zoomSpeedIn			: 500,
	zoomSpeedOut		: 500,
	frameHeight			: 400, // min-height
	hideOnContentClick	: false,
	callbackOnShow		: function() {
		stopLoadingIcon();
		$j("a.fancybox-button").blur();
	}
});

// ADDING NEW IMAGES
$j("td#toolbar-add a").attr('href','<?php echo TRUE_INDEX2.'&view=upload&task=view&'.TG_AX; ?>');
$j("td#toolbar-add a").attr('onclick','').fancybox({
	'zoomOpacity'			: true,
	'overlayShow'			: true,
	'easingIn'				: 'easeOutBack',
	'easingOut'				: 'easeInBack',
	'zoomSpeedIn'			: 300,
	'zoomSpeedOut'			: 300,
	'frameWidth'			: 550,
	'frameHeight'			: 415, // min-height
	'hideOnContentClick'	: false,
	'callbackOnShow'		: function() {
		stopLoadingIcon();
		$j(this).blur();
	},
	'callbackOnClose'		: function() {
		stopLoadingIcon();
	}
});

// PUBLISHING and APPROVING
$j('div.publish').publish(); $j('div.unpublish').publish(); // preparing image buttons on 
$j('div.approve').approve(); $j('div.reject').approve(); // each TR-line

//clearing "onclick" property for PUBLISH button
//$j("td#toolbar-publish a").attr('onclick','');
//$j("td#toolbar-unpublish a").attr('onclick','');
// interesting 'bug' - if set function for attribute 'onclick' 
// then it raise immidiately. strange...

// toolbar-menu icons Publish/Unbublish
$j("td#toolbar-publish a").attr('onclick','').click(function(event){
	$j("tr:has(input:checked) div.unpublish").publish(1);
	$j("tr input:checked").each(function(){
		//$j(this).removeAttr( 'checked' );
	});
});
$j("td#toolbar-unpublish a").attr('onclick','').click(function(event){
	$j("tr:has(input:checked) div.publish").publish(0);
	$j("tr input:checked").each(function(){
		//$j(this).removeAttr( 'checked' );
	});
});
// toolbar-menu icons Approve/Reject
$j("td#toolbar-approve a").attr('onclick','').click(function(event){
	$j("tr:has(input:checked) div.reject").approve(1);
	$j("tr input:checked").each(function(){
		//$j(this).removeAttr( 'checked' );
	});
});
$j("td#toolbar-reject a").attr('onclick','').click(function(event){
	//event.preventDefault();
	$j("tr:has(input:checked) div.approve").approve(0);
	$j("tr input:checked").each(function(){
		//$j(this).removeAttr( 'checked' );
	});
});

$j("#showoverlib").click( function(){
	var checked = $j(this).attr('checked') ? 1 : 0;
	var f = $j('<form name="showovlform" method="post" action="<?php echo TRUE_INDEX2; ?>&view=pictures&task=setoverlib&ax=1&no_html=1" ></form>');
	f.html('<input name="showoverlib" type="hidden" value="'+checked+'" />');
	f.ajaxSubmit(function(){
		success: stopLoadingIcon()
	});
	//f.submit();
});

<?php echo "\n".$pid_codes; ?>
prependOverlib( 'tr.trover', pid_codes );
//-->
</script>
<?php
//getPlugin('eafupload', true);
?>